package com.csdj.questions.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import com.csdj.questions.entity.AttentionQuestion;
import com.csdj.questions.entity.TQuestion;
import com.csdj.questions.entity.TQuestionTUsers;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 问题表 Mapper 接口
 * </p>
 *
 * @author 刘宇
 * @since 2022-03-02
 */
public interface TQuestionMapper extends BaseMapper<TQuestion> {
    /**
     * 连表查询 查user用户表和TQuestion评论表
     * @return
     */
    @Select("SELECT u.`photo`,q.`create_time`,`name`,q.`ques_name`,q.`answer_numb`,q.`attention_numb`\n" +
            "FROM t_users AS u\n" +
            "LEFT JOIN t_question AS q\n" +
            "ON u.userId= q.user_id;")
    public List<TQuestionTUsers> selectTQuestionAll();

    /**
     * 连表查询 查user用户表和TQuestion评论表 并按照时间降序来排列
     * @return
     */
    @Select("SELECT u.`photo`,q.`create_time`,`name`,q.`ques_name`,q.`answer_numb`,q.`attention_numb`\n" +
            "FROM t_users AS u\n" +
            "LEFT JOIN t_question AS q\n" +
            "ON u.userId= q.user_id\n" +
            "ORDER by q.create_time DESC;")
    public List<TQuestionTUsers> selectTQUTimeDesc();

    /**
     * 连表查询 查user用户表和TQuestion评论表 并按照访问人数降序来排列
     * @return
     */
    @Select("SELECT u.`photo`,q.`create_time`,`name`,q.`ques_name`,q.`answer_numb`,q.`attention_numb`,q.`browse_numb`\n" +
            "FROM t_users AS u\n" +
            "LEFT JOIN t_question AS q\n" +
            "ON u.userId= q.user_id\n" +
            "ORDER by q.browse_numb DESC;")
    public List<TQuestionTUsers> selectTQUBrowseNumbDesc();



    public  List<TQuestionTUsers> selQuestion(@Param("QuestionId") String QuestionId);


    /**
     * 多表查询，根据用户表的id
     * @return
     */
    @Select("SELECT ques_name\n" +
            "FROM t_users AS u\n" +
            "JOIN attention_question AS a\n" +
            "ON a.user_id = u.userid\n" +
            "JOIN t_question AS q\n" +
            "ON a.ques_id = q.ques_id\n" +
            "WHERE u.Id = #{uid}")
    public List<AttentionQuestion> selectName(int uid);
}
